#include "xlcomm/base/process.h"

#ifdef _WIN32
#include <process.h>
#include <windows.h>
#include <winsock.h>
#pragma comment(lib, "Ws2_32")
#else
#include <unistd.h>
#endif  // _WIN32

namespace xlcomm {

uint64_t Process::GetProcessId() {
#ifdef _WIN32
  DWORD pid = ::GetCurrentProcessId();
  return static_cast<uint64_t>(pid);
#else
  return static_cast<uint64_t>(::getpid());
#endif  // _WIN32
}

std::string Process::GetHostName() {
#ifdef _WIN32
  WSAData data;
  if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
    return "unknown";
  }
#endif  // _WIN32

  char buff[256];
  if (::gethostname(buff, sizeof(buff)) == 0) {
    buff[sizeof(buff) - 1] = '\0';
  } else {
    Strncpy(buff, "unknownhost", sizeof buff);
  }

#ifdef _WIN32
  WSACleanup();
#endif  // _WIN32
  return buff;
}

}  // namespace xlcomm